// To change this License template, choose Tools / Templates
// and edit Licenses / FanDefaultLicense.txt
//
// History:
//   15.02.2011 JSTAUDEMEYER Creation
//

using gfx
using fwt
using xml

**
** Main
**
class Main : TreeModel
{
 
  **
  ** Main method
  **
  static Void main()
  {
    WebBrowser? page
    Window
    {
      title = "Fantom Pod Browser"
      size = Size(300,200)
      SashPane {
          weights = [20,80]
          //BorderPane {
            //border = Border("1")
            Tree {
              model = Main()
              onSelect.add |event| { page.loadStr(showObject(event.data)) }
            },
          //},
          //BorderPane {
            //border = Border("1")
            page = WebBrowser()
          //},
      },
    }.open
  }
  
  
  static Str showObject(Obj obj) {
    echo(obj)
    XDoc? doc
    if (obj is Pod) {
      doc = showPod((Pod)obj)
    }
    Str? result
    if (doc!=null) {
      buf := StrBuf()
      doc.write(buf.out)
      result = buf.toStr
    } else {
      result = obj.toStr
    }
    echo(result)
    return result
  }

  static XDoc showPod(Pod pod) {
    doc := XDoc {
      XElem("html") {
        XElem("h1") { XText("Pod: $pod"), },
        XElem("table") {
          XAttr("border",1),
          XAttr("cellspacing",0),
          trtd(["Version",pod.version]),
          trtd(["Uri:",pod.uri.toStr]),
          XElem("tr") {
            td("Depends:")
            pod.depends.each |dep| {
              page.A(Uri("?pod=$dep.name")) { dep.toStr, }
            }
          },
        }
        XElem("h2") {XText("Meta")},
        if (!pod.meta.isEmpty) {
          XElem("ul") {
            pod.meta.each |v,k| { add( li("$k = $v") }
          }
        }
        XElem("hr")
      if(pod.doc!=null) parseDoc(pod.doc.in,page)
    }
    */
  }
  
  private XElem trtd(Obj[] values) {
    tr := XElem("tr")
    values.each { tr.add(XElem("td"){XText(it),} )  }
    return tr
  }

  private XElem trth(Obj[] values) {
    tr := XElem("tr")
    values.each { tr.add(XElem("th"){XText(it),} )  }
    return tr
  }

  private XElem td(Obj value) {
    XElem("tr") {XText(value),}
  }

  private XElem li(Object value) {
    XElem("li") {XText(value)}
  } 
 
  override Obj[] children(Obj node) {
    if (node is Pod) {
      return ((Pod)node).types.findAll{!it.name.contains("\$")}
    } else if (node is Type) {
      return ((Type)node).slots
    } else {
      return [,]
    }
  }
  
  override Bool hasChildren(Obj node) {
    return node is Pod || node is Type
  }

  override Obj[] roots() { Pod.list }  
 

  override Str text(Obj node) {
    node->name
  }
  
}